#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDBusMessage>
#include <QDBusConnection>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //QDBusConnection::sessionBus().connect("org.ukui.IndexFrameworkSessionService", "/", "org.ukui.IndexFrameworkSessionService", "messageReceived", this, SLOT(handleServiceMessage(QString)));
    bool ok = QDBusConnection::sessionBus().connect("org.ukui.SessionFileMonitor", "/", "org.ukui.SessionFileMonitor", "fileEvent", this, SLOT(handleFileEvent(quint32,QString,quint32,QString,quint64,QString,QString,quint64,QString)));
    qDebug() << "setup file monitor signal connection" << ok;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleServiceMessage(QByteArray msg)
{
    ui->textEdit->append(msg);
}

void MainWindow::handleFileEvent(quint32 pid, QString command, quint32 eventType, QString srcUUID, quint64 srcId, QString srcPath, QString dstUUID, quint64 dstId, QString dstPath)
{
    static const char *typeName[] = {"FileCreate", "FileDelete", "FileRename", "FileContentChange", "FilePermissionChange"};
    QByteArray data;
    QTextStream stream(&data);
    stream << "=================file event==========================";
    stream << "pid:" << pid << "command:" << command << "event type:" << typeName[eventType] << "\n";
    if (!srcPath.isEmpty()) {
        stream << "src info, uuid:" << srcUUID << srcId << srcPath << "\n";
    } else {
        return;
    }
    if (!dstPath.isEmpty()) {
        stream << "dst info, uuid:" << dstUUID << dstId << dstPath << "\n";
    }
    stream.flush();

    QString text(data);
    ui->textEdit->append(text);
}
